package cn.org.mrliu.ssm.util;

import java.io.IOException;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

public class PaginationTag extends TagSupport {
	private static final long serialVersionUID = 6361788393761774763L;
	private Pagination pagination;
	// private Integer pageIndex;
	// private Integer pageSize;
	// private Integer totalRows;
	// private Integer totalPages;
	// private String pageSelect;
	// private List<?> list;
	// private String line = "\n";
	private String url;

	public PaginationTag() {
		System.out.println("\n\nPaginationTag 构造函数.....\n\n");
	}

	/**
	 * <pre>
		<!-- pagination start-->
		<div class=\"row pagination-wrap\">
		              <div class=\"col-sm-6 pagination-info\">
		                当前第1页/总计100页(1000行),每页显示
		                <select name=\"\" id=\"\" class=\"form-control page-size\">
		                  <option value=\"\">10</option>
		                  <option value=\"\">20</option>
		                  <option value=\"\">50</option>
		                  <option value=\"\">100</option>
		                  <option value=\"\">200</option>
		                  <option value=\"\">500</option>
		                </select>
		                行
		
		                &nbsp;&nbsp;<input type=\"text\" class=\"form-control jump-input\"/>
		                <button class=\"btn btn-default btn-sm jump-btn\">GO</button>
		
		              </div>
		              <div class=\"col-sm-6 pagination-page\">
		                <nav>
		                  <ul class=\"pagination\">
		                    <li class=\"disabled\"><a href=\"#\" title=\"首页\">«</a></li>
		                    <li class=\"disabled\"><a href=\"#\">‹</a></li>
		                    <li class=\"active\"><a href=\"#\">1 <span class=\"sr-only\">(current)</span></a></li>
		                    <li><a href=\"${pageContext.request.contextPath}/employee/list?pageIndex=2&pageSize=10\">2</a></li>
		                    <li><a href=\"${pageContext.request.contextPath}/employee/list?pageIndex=3&pageSize=10\">3</a></li>
		                    <li><a href=\"${pageContext.request.contextPath}/employee/list?pageIndex=4&pageSize=10\">4</a></li>
		                    <li><a href=\"${pageContext.request.contextPath}/employee/list?pageIndex=5&pageSize=10\">5</a></li>
		                    <li><a href=\"${pageContext.request.contextPath}/employee/list?pageIndex=6&pageSize=10\">6</a></li>
		                    <li><a href=\"${pageContext.request.contextPath}/employee/list?pageIndex=7&pageSize=10\">7</a></li>
		                    <li><a href=\"${pageContext.request.contextPath}/employee/list?pageIndex=8&pageSize=10\">8</a></li>
		                    <li><a href=\"#\">›</a></li>
		                    <li><a href=\"#\">»</a></li>
		                  </ul>
		                </nav>
		              </div>
		          </div>
		<!-- pagination end-->
	 * </pre>
	 */
	@Override
	public int doStartTag() throws JspException {
		System.out.println("doStartTag.......");
		if (pagination == null) {
			return TagSupport.SKIP_PAGE;// 不显示分页
		}

		StringBuffer page = new StringBuffer();

		page.append("<!-- pagination start-->\n");
		page.append("<div class=\"row pagination-wrap\">");
		// ------------------------------------- 分页信息start
		// -------------------------------------
		page.append("<div class=\"col-sm-6 pagination-info\">");
		page.append("当前第" + pagination.getPageNum() + "页/总计" + pagination.getPages() + "页(" + pagination.getTotal()
				+ "行),每页显示");
		page.append("<select name=\"\" id=\"\" class=\"form-control page-size\">");
		page.append("<option value=\"\">10</option>");
		page.append("<option value=\"\">20</option>");
		page.append("<option value=\"\">50</option>");
		page.append("<option value=\"\">100</option>");
		page.append("<option value=\"\">200</option>");
		page.append("<option value=\"\">500</option>");
		page.append("</select>");
		page.append("行");

		page.append("&nbsp;&nbsp;<input type=\"text\" class=\"form-control jump-input\"/>");
		page.append("<button class=\"btn btn-default btn-sm jump-btn\">GO</button>");

		page.append("</div>");
		// ------------------------------------- 分页信息start
		// -------------------------------------

		page.append("<div class=\"col-sm-6 pagination-page\">");
		page.append("<nav>");
		page.append("<ul class=\"pagination\">");

		// 是否有首页和上一页
		if (pagination.isHasPreviousPage()) {// 有
			page.append("<li><a href=\"" + url + "?pageIndex=" + 1 + "&pageSize="
					+ pagination.getPageSize() + "\" title=\"首页\">«</a></li>");
			page.append("<li><a href=\"" + url + "?pageIndex=" + pagination.getPrePage() + "&pageSize="
					+ pagination.getPageSize() + "\" title=\"上一页\">‹</a></li>");
		} else {
			page.append("<li class=\"disabled\"><a href=\"javascript:;\">«</a></li>");
			page.append("<li class=\"disabled\"><a href=\"javascript:;\">‹</a></li>");
		}

		int[] pageNums = pagination.getNavigatepageNums();
		int pageNum = pagination.getPageNum();
		String currentPageIndexClass = "";
		for (int pageIndex : pageNums) {
			if (pageIndex == pageNum) {
				currentPageIndexClass = "active";
			} else {
				currentPageIndexClass = "";
			}
			page.append("<li class=\"" + currentPageIndexClass + "\"><a href=\"" + url + "?pageIndex=" + pageIndex
					+ "&pageSize=" + pagination.getPageSize() + "\">" + pageIndex
					+ "</a></li>");

		}

		if (pagination.isHasNextPage()) {// 有
			page.append("<li><a href=\"" + url + "?pageIndex=" + pagination.getNextPage() + "&pageSize="
					+ pagination.getPageSize() + "\" title=\"下一页\">›</a></li>");
			page.append("<li><a href=\"" + url + "?pageIndex=" + pagination.getPages() + "&pageSize="
					+ pagination.getPageSize() + "\" title=\"尾页\">»</a></li>");
		} else {
			page.append("<li class=\"disabled\"><a href=\"javascript:;\">›</a></li>");
			page.append("<li class=\"disabled\"><a href=\"javascript:;\">»</a></li>");
		}

		page.append("</ul>");
		page.append("</nav>");
		page.append("</div>");
		page.append("</div>");
		page.append("\n<!-- pagination end-->");

		System.out.println(pagination);
		JspWriter out = pageContext.getOut();
		try {
			out.print(page);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doEndTag();
	}

	public Pagination getPagination() {
		return pagination;
	}

	public void setPagination(Pagination pagination) {
		this.pagination = pagination;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	@Override
	public int doAfterBody() throws JspException {
		System.out.println("doAfterBody..........");
		return super.doAfterBody();
	}

	@Override
	public int doEndTag() throws JspException {
		System.out.println("doEndTag..........");
		return super.doEndTag();
	}

}
